package toy.keli.edic.ui.main

import android.app.Activity
import android.util.Log
import androidx.compose.animation.core.Animatable
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import toy.keli.edic.MainActivity
import toy.keli.edic.data.GlobalVal
import toy.keli.edic.data.UserStatus
import toy.keli.edic.data.config.MenuConf
import toy.keli.edic.ui.bookmark.BookmarkScreen
import toy.keli.edic.ui.bookmark.BookmarkViewModel
import toy.keli.edic.ui.bookmark.data.BookMark
import toy.keli.edic.ui.history.HistoryScreen
import toy.keli.edic.ui.history.HistoryViewModel
import toy.keli.edic.ui.home.HomeEvents
import toy.keli.edic.ui.home.HomeScreen
import toy.keli.edic.ui.home.JdcScreen
import toy.keli.edic.ui.home.PAGE_FROM_HISTORY
import toy.keli.edic.ui.home.PAGE_FROM_HOME
import toy.keli.edic.ui.home.PAGE_FROM_LEVEL
import toy.keli.edic.ui.home.WordModelViewModel
import toy.keli.edic.ui.home.game.LineGameScreen
import toy.keli.edic.ui.home.homescreen.SwitchMainPage
import toy.keli.edic.ui.level.LevelScreen
import toy.keli.edic.ui.level.LevelViewModel
import toy.keli.edic.ui.listenbook.BookShowScreen
import toy.keli.edic.ui.listenbook.ListenBookScreen
import toy.keli.edic.ui.listenbook.ListenScreen
import toy.keli.edic.ui.main.components.Display
import toy.keli.edic.ui.main.components.Display.getBitMapByPageScreen
import toy.keli.edic.ui.main.components.Display.px2dp
import toy.keli.edic.ui.main.components.Display.setBitMapByPageScreen
import toy.keli.edic.ui.main.components.InfoDialog
import toy.keli.edic.ui.navigation.NavScreen
import toy.keli.edic.util.ActivityRun
import toy.keli.edic.util.ActivityRun.Companion.context
import kotlin.math.abs
import kotlin.system.exitProcess


@Composable
fun MainScreen(
    navController: NavHostController,
    scaffoldState: androidx.compose.material.ScaffoldState,
    toggleTheme: () -> Unit = { Unit }
) {
    val levelViewModel: LevelViewModel = hiltViewModel()
    val wordViewModel: WordModelViewModel = hiltViewModel()
    val bookmarkViewModel: BookmarkViewModel = hiltViewModel()
    val historyViewModel: HistoryViewModel = hiltViewModel()

    GlobalVal.bookmarkViewModel = bookmarkViewModel
    GlobalVal.nav = navController
    GlobalVal.wordViewModel = wordViewModel
    GlobalVal.clipboardManager = LocalClipboardManager.current

    val fullWidth = with(LocalContext. current) {
        resources. displayMetrics. widthPixels
    }
    val dragAble = UserStatus.get{
//        val defv = !ActivityRun.isHorizontalScreens()
        it.getBoolean("HorizontalDrawAble",true)
    }
    val dragEnabled = remember {
        mutableStateOf(false)
    }
    var dragDistance by remember { mutableStateOf(0F) }
    var pageIndex by remember {
        mutableStateOf(1)
    }
    val dragEvent = remember{
        object {
            val move:(y:Float) ->Unit ={
                if(pageIndex<1 && it>0) {
                        //最左侧
                }else if(pageIndex>=NavScreen.rotesList.size-1 && it<0) {
                        //最右侧
                }else if(dragAble || dragEnabled.value)
                    dragDistance+=it
            }
            val onDragStarted: (y:Offset)->Unit = { offset ->
                dragDistance = 0f
                setBitMapByPageScreen((pageIndex).toString())
            }
        }
    }

    // 监听屏幕方向变化
    val lifecycle = LocalLifecycleOwner.current.lifecycle
    DisposableEffect(context, lifecycle) {
        val observer = LifecycleEventObserver { _, event ->
            if (event == Lifecycle.Event.ON_CREATE) {
                val isHs = ActivityRun.isHorizontalScreens()
                if(isHs != Display.isHorizontalScreens.value){
                    Display.isHorizontalScreens.value = isHs
                    MenuConf.tryUpDefConfig()
                }
            }
        }
        lifecycle.addObserver(observer)
        onDispose {
            lifecycle.removeObserver(observer)
        }
    }

    Box(modifier = Modifier
        .fillMaxSize()
        .offset(x = px2dp(dragDistance.toInt()))
        .padding(top=20.dp)
        .draggable(
            enabled = false &&  dragAble && HomeEvents.downMenuOpen,
            state = rememberDraggableState(onDelta = {
                dragEvent.move(it)
            }),
            orientation = Orientation.Horizontal,
            onDragStarted = {
                dragEvent.onDragStarted(it)
            },
            onDragStopped = { offset ->
                if (abs(dragDistance) > fullWidth / 5) {
                    val cOrientation = if (dragDistance > 0) -1 else 1
                    pageIndex = NavScreen.toPage(cOrientation)
                }
                Animatable(dragDistance).animateTo(0f) {
                    dragDistance = value
                }
            }
        )
    ){
        InfoDialog.add()
        //左侧窗口
        Box(modifier = Modifier
            .fillMaxSize()
            .offset(x = px2dp(0 - fullWidth), 0.dp)
            .background(MaterialTheme.colorScheme.surface)){
                Image(painter = BitmapPainter(getBitMapByPageScreen((pageIndex-1).toString())), contentDescription =null, modifier = Modifier.fillMaxSize() )
            }
        //右侧窗口
        Box(modifier = Modifier
            .fillMaxSize()
            .offset(x = px2dp(fullWidth), 0.dp)
            .background(MaterialTheme.colorScheme.surface)){
                Image(painter = BitmapPainter(getBitMapByPageScreen((pageIndex+1).toString())), contentDescription =null, modifier = Modifier.fillMaxSize() )
            }
        NavHost(
            navController = navController,
            startDestination = "${NavScreen.HomeScreen.route}?wordIndex={wordIndex}?fromPage={fromPage}?level={level}"
        ) {
        composable(
            route = "${NavScreen.HomeScreen.route}?wordIndex={wordIndex}?fromPage={fromPage}?level={level}",
            arguments = listOf(
                navArgument("wordIndex") {
                    type = NavType.IntType
                    defaultValue = -1
                },
                navArgument("fromPage") {
                    type = NavType.IntType
                    defaultValue = PAGE_FROM_HOME
                },
                navArgument("level") {
                    type = NavType.IntType
                    defaultValue = -1
                }
            )
        ) {
            HomeScreen(
                navController,
                it.arguments?.getInt("wordIndex", -1),
                it.arguments?.getInt("fromPage", PAGE_FROM_HOME) ?: PAGE_FROM_HOME,
                wordViewModel, bookmarkViewModel, historyViewModel,
                onToggleTheme = toggleTheme,
                le = it.arguments?.getInt("level", -1)
            )
        }

        composable(route = NavScreen.LevelScreen.route) {
            LevelScreen(levelViewModel) { index, entity ->
                val newLevel = entity.level ?: -1
                Log.d("LevelScreen", "select new level = $newLevel")
                navController.navigate("${NavScreen.HomeScreen.route}?wordIndex=$index?fromPage=$PAGE_FROM_LEVEL?level=${entity.level}") {
                    launchSingleTop = true
                }
            }

            //Text("level Box")
        }

        composable(route = NavScreen.BookmarkScreen.route+"?pid={pid}",
            arguments = listOf(
                navArgument("pid") {
                    type = NavType.IntType
                    defaultValue = 0
                })
        ) {
            val pid = it.arguments?.getInt("pid",0)
            if (pid != null) {
                BookMark.changePid(pid)
            }
            BookmarkScreen()
//            BookmarkScreen(bookmarkViewModel) { index ->
//                navController.navigate("${NavScreen.HomeScreen.route}?wordIndex=$index?fromPage=$PAGE_FROM_BOOKMARK?level=${-1}") {
//                    launchSingleTop = true
//                }
//            }
        }

        composable(route = NavScreen.HistoryScreen.route+"?type={type}",
                arguments = listOf(
                    navArgument("type") {
                        type = NavType.IntType
                        defaultValue = -1
                    })
        ) {
            HistoryScreen(it.arguments,historyViewModel) { index ->
                navController.navigate("${NavScreen.HomeScreen.route}?wordIndex=$index?fromPage=${PAGE_FROM_HISTORY}?level=${-1}") {
                    launchSingleTop = true
                }
            }

//            Text("History Box")
        }

        composable(route = NavScreen.routes.ListenBooks+"?aids={aids}",
            arguments = listOf(
                navArgument("aids") {
                    type = NavType.StringType
                    defaultValue = ""
                })
        ) {
            ListenScreen(it.arguments)
//            {
//                navController.navigate("${NavScreen.routes.ListenBook}") {
//                    launchSingleTop = true
//                }
//            }
        }
//        composable(route = NavScreen.routes.Setup) {
//            val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
////            if (mBluetoothAdapter == null) {
////                textView.setText("Missing bluetooth device.")
////                return@composable
////            }
//            val intentBluetooth = Intent()
//            intentBluetooth.setAction(Settings.ACTION_BLUETOOTH_SETTINGS)
//            ActivityRun.context().startActivity(intentBluetooth)
////            val intent: Intent = Intent(getActivity(), MainActivity::class.java)
////            getActivity().finish()
//        }


        composable(route = NavScreen.routes.Setting,
        ) {
            SettingScreen()
        }
        composable(route = NavScreen.routes.Quit) {
            val act = ActivityRun.context() as Activity
            act.finish()
            exitProcess(0)
        }
        //单词表现页
        composable(route = NavScreen.routes.Switch) {
            SwitchMainPage()
        }
        //单词表现页
        composable(route = NavScreen.routes.LineGame+"?words={words}",arguments = listOf(
            navArgument("words") {
                type = NavType.StringListType
                defaultValue = listOf<String>()
            })) {
            val words = when(it.arguments){
                null ->   listOf<String>()
                else -> {
                    val ws = it.arguments?.getString("words","")
                    ws?.split(",")
                }
            }
            if (words != null) {
                LineGameScreen(words)
            }
        }
        //单词表现页
        composable(route = NavScreen.routes.ListenBook+"?words={words}",arguments = listOf(
            navArgument("words") {
                type = NavType.StringListType
                defaultValue = listOf<String>()
            })) {
            val args = it.arguments
            val words = when(args){
                null ->   listOf<String>()
                else -> {
                    val ws = args?.getStringArray("words")
                    ws!!.map{it}
                }
            }
            if (words != null) {
                ListenBookScreen(words)
            }
        }
        composable(route = NavScreen.routes.Jdc+"?page={page}",
            arguments = listOf(
                navArgument("page") {
                    type = NavType.IntType
                    defaultValue = 0
                })
        ) {
            JdcScreen(it.arguments!!.getInt("page",0))
        }
        composable(route = NavScreen.routes.About+"?page={page}",arguments = listOf(
            navArgument("page") {
                type = NavType.StringType
                defaultValue = "" })
        ) {
            when(it.arguments!!.getString("page","")){
                "" -> AboutScreen()
                "privacy" -> PrivacyScreen()
            }
        }

        composable(route = NavScreen.routes.showBook + "?bid={bid}",arguments = listOf(
            navArgument("bid") {
                type = NavType.IntType
                defaultValue = 0 })
        ) {
            val bid = it.arguments!!.getInt("bid",0)
            BookShowScreen(bid)
        }
    }
    }
}